package com.youkeyi.ddy.cloud.application.business.crm.customer.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.crm.customer.BusinessCustomerQuickCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.customer.param.BusinessCustomerCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.customer.param.BusinessQuickCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.order.param.BusinessCustomerOrderCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.CrmCustomerCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.dto.CrmCustomerListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.param.CrmCustomerCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.customer.param.CrmCustomerQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.CrmCustomerOrderCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.param.CrmCustomerOrderCreateCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.CheckUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;

/**
 * BusinessCustomerQuickController
 *
 * @Author liuxiawang
 * @Date 2024/3/11 3:33 PM
 * @Location shenzhen.china
 */
@Service
public class BusinessCustomerQuickController implements BusinessCustomerQuickCloudService {

    @Autowired
    private CrmCustomerCloudService crmCustomerCloudService;
    @Autowired
    private CrmCustomerOrderCloudService crmCustomerOrderCloudService;

    @Override
    public ResponseContent create(BusinessQuickCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || null == param.getCustomerInfo() || null == param.getOrderInfo()
                || (StringUtils.isBlank(param.getCustomerInfo().getName()) || StringUtils.isBlank(param.getCustomerInfo().getPhone()))
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        BusinessCustomerCreateCloudParam customerInfo = param.getCustomerInfo();
        if (StringUtils.isNotBlank(customerInfo.getEmail())) {
            if (!CheckUtils.isValidEmain(customerInfo.getEmail())) {
                rc.setErrorMessage(ResponseCodeEnum.CRM_EMAIL_INVALID.getMessage());
                return rc;
            }
        }

        String customerPrimaryKey;
        CrmCustomerQueryCloudParam customerQueryCloudParam = new CrmCustomerQueryCloudParam();
        customerQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        customerQueryCloudParam.setName(customerInfo.getName());
        customerQueryCloudParam.setPhone(customerInfo.getPhone());
        CrmCustomerListCloudDto customerDtos = crmCustomerCloudService.query(customerQueryCloudParam);
        if (null != customerDtos && CollectionUtils.isNotEmpty(customerDtos.getRows())) {
            customerPrimaryKey = customerDtos.getRows().get(0).getCustomerPrimaryKey();
        } else {
            CrmCustomerCreateCloudParam customerCreateCloudParam = new CrmCustomerCreateCloudParam();
            customerCreateCloudParam.setCsCorpKey(customerInfo.getCsCorpKey());
            customerCreateCloudParam.setCsUserKey(customerInfo.getCsUserKey());
            customerCreateCloudParam.setCsUserName(customerInfo.getCsUserName());
            customerCreateCloudParam.setSourceCorpKey(customerInfo.getSourceCorpKey());
            customerCreateCloudParam.setSourceUserKey(customerInfo.getSourceUserKey());
            customerCreateCloudParam.setSourceUserName(customerInfo.getSourceUserName());
            customerCreateCloudParam.setSourceChannel(customerInfo.getSourceChannel());
            customerCreateCloudParam.setAdviserCorpKey(customerInfo.getAdviserCorpKey());
            customerCreateCloudParam.setAdviserUserKey(customerInfo.getAdviserUserKey());
            customerCreateCloudParam.setAdviserUserName(customerInfo.getAdviserUserName());
            customerCreateCloudParam.setServerCorpKey(customerInfo.getServerCorpKey());
            customerCreateCloudParam.setServerUserKey(customerInfo.getServerUserKey());
            customerCreateCloudParam.setServerUserName(customerInfo.getServerUserName());
            customerCreateCloudParam.setCustomerTypeKey(customerInfo.getCustomerTypeKey());
            customerCreateCloudParam.setName(customerInfo.getName());
            customerCreateCloudParam.setWechatId(customerInfo.getPhone());
            customerCreateCloudParam.setSex(customerInfo.getSex());
            customerCreateCloudParam.setHeight(customerInfo.getHeight());
            customerCreateCloudParam.setWeight(customerInfo.getWeight());
            customerCreateCloudParam.setBirthday(customerInfo.getBirthday());
            customerCreateCloudParam.setPhone(customerInfo.getPhone());
            customerCreateCloudParam.setIdCard(customerInfo.getIdCard());
            customerCreateCloudParam.setEmail(customerInfo.getEmail());
            customerCreateCloudParam.setAddress(customerInfo.getAddress());
            customerCreateCloudParam.setEmergencyContactName(customerInfo.getEmergencyContactName());
            customerCreateCloudParam.setEmergencyContactPhone(customerInfo.getEmergencyContactPhone());
            customerCreateCloudParam.setEmergencyContactRelationship(customerInfo.getEmergencyContactRelationship());
            customerCreateCloudParam.setCompany(customerInfo.getCompany());
            customerCreateCloudParam.setJobTitle(customerInfo.getJobTitle());
            customerCreateCloudParam.setIndustry(customerInfo.getIndustry());
            customerCreateCloudParam.setAccountType(customerInfo.getAccountType());
            customerCreateCloudParam.setAccountName(customerInfo.getAccountName());
            customerCreateCloudParam.setBank(customerInfo.getBank());
            customerCreateCloudParam.setBankNo(customerInfo.getBankNo());
            customerCreateCloudParam.setLicenseName(customerInfo.getLicenseName());
            customerCreateCloudParam.setLicenseCode(customerInfo.getLicenseCode());
            customerCreateCloudParam.setIncomeRmb(customerInfo.getIncomeRmb());
            customerCreateCloudParam.setIncomeHkd(customerInfo.getIncomeHkd());
            customerCreateCloudParam.setLeadSource(customerInfo.getLeadSource());
            customerCreateCloudParam.setPotentialDemandKey(customerInfo.getPotentialDemandKey());
            customerCreateCloudParam.setPotentialDemandName(customerInfo.getPotentialDemandName());
            customerCreateCloudParam.setProvince(customerInfo.getAddress());
            customerCreateCloudParam.setCity(customerInfo.getCity());
            customerCreateCloudParam.setAdviserFlag(customerInfo.getAdviserFlag());
            customerCreateCloudParam.setAdviserFlagUserKey(customerInfo.getAdviserFlagUserKey());
            customerCreateCloudParam.setAdviserFlagUserName(customerInfo.getAdviserFlagUserName());
            customerCreateCloudParam.setCsFlag(customerInfo.getCsFlag());
            customerCreateCloudParam.setCsFlagUserKey(customerInfo.getCsFlagUserKey());
            customerCreateCloudParam.setCsFlagUserName(customerInfo.getCsFlagUserName());
            customerCreateCloudParam.setPlace(customerInfo.getPlace());
            customerCreateCloudParam.setInitialFollowTime(customerInfo.getInitialFollowTime());
            customerCreateCloudParam.setInitialFollowDesc(customerInfo.getInitialFollowDesc());
            customerCreateCloudParam.setRemark(customerInfo.getRemark());
            customerCreateCloudParam.setEvaluationDate(customerInfo.getEvaluationDate());
            customerCreateCloudParam.setVisaExpirationDate(customerInfo.getVisaExpirationDate());
            customerCreateCloudParam.setAnnexKey(customerInfo.getAnnexKey());
            customerCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
            customerCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
            ResponseContent customerCreateDto = crmCustomerCloudService.create(customerCreateCloudParam);
            if (null != customerCreateDto && StringUtils.isNotBlank(customerCreateDto.getKey())) {
                customerPrimaryKey = customerCreateDto.getKey();
            } else {
                return customerCreateDto;
            }
        }

        BusinessCustomerOrderCreateCloudParam orderInfo = param.getOrderInfo();

        CrmCustomerOrderCreateCloudParam customerOrderCreateCloudParam = new CrmCustomerOrderCreateCloudParam();
        customerOrderCreateCloudParam.setCorpKey(corpKey);
        customerOrderCreateCloudParam.setFinanceCorpKey(orderInfo.getFinanceCorpKey());
        customerOrderCreateCloudParam.setCustomerKey(customerPrimaryKey);
        customerOrderCreateCloudParam.setCustomerName(customerInfo.getName());
        customerOrderCreateCloudParam.setServiceSectionKey(orderInfo.getServiceSectionKey());
        customerOrderCreateCloudParam.setServiceSectionName(orderInfo.getServiceSectionName());
        customerOrderCreateCloudParam.setServiceItemsKey(orderInfo.getServiceItemsKey());
        customerOrderCreateCloudParam.setServiceItemsName(orderInfo.getServiceItemsName());
        customerOrderCreateCloudParam.setServiceUserKey(orderInfo.getServiceUserKey());
        customerOrderCreateCloudParam.setServiceUserName(orderInfo.getServiceUserName());
        customerOrderCreateCloudParam.setIncomeRmb(orderInfo.getIncomeRmb());
        customerOrderCreateCloudParam.setIncomeHkd(orderInfo.getIncomeHkd());
        customerOrderCreateCloudParam.setAnnexKey(orderInfo.getAnnexKey());
        customerOrderCreateCloudParam.setRemark(orderInfo.getRemark());
        customerOrderCreateCloudParam.setSourceDepartmentKey(orderInfo.getSourceDepartmentKey());
        customerOrderCreateCloudParam.setSourceDepartmentName(orderInfo.getSourceDepartmentName());
        customerOrderCreateCloudParam.setSourceUserName(orderInfo.getSourceUserName());
        customerOrderCreateCloudParam.setSourceUserKey(orderInfo.getSourceUserKey());
        customerOrderCreateCloudParam.setRegistrationTime(orderInfo.getRegistrationTime());
        customerOrderCreateCloudParam.setPotentialDemandKey(orderInfo.getPotentialDemandKey());
        customerOrderCreateCloudParam.setPotentialDemandName(orderInfo.getPotentialDemandName());
        customerOrderCreateCloudParam.setStatus(orderInfo.getStatus());
        customerOrderCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        customerOrderCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return crmCustomerOrderCloudService.create(customerOrderCreateCloudParam);
    }
}
